home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip1292.zip / REGEX.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  3KB  |  89 lines

  1. /*
  2. ** By: Orv Stoll, 1987
  3. **
  4. **
  5. ** The match() routine below does wildcard matches with a superset of MSDOS
  6. ** wildcard elements. The "*" replaces any number of characters including no
  7. ** characters. The "?" replaces exactly one character and there must be a
  8. ** character in that position. The "[0-9]" construct means that that character
  9. ** location must have a range from the value of the first character (0) to the
  10. ** value of the second character (9). In  this case an ascii digit. [A-M]
  11. ** would be an upper case A through M.
  12. */
  13.  
  14. main()
  15. {
  16.       test("teststring","*tst*");
  17.       test("bcdfjewoifj.xxx","*.x???");
  18.       test("anotheRstring","*[A-Z]*");
  19.       test("checker.txt","*k*.t?t");
  20. }
  21.  
  22. test(str,pat) char *str,*pat;
  23. {
  24.       char *ans;
  25.       printf("%s %s ",str,pat);
  26.       if (match(str,pat))
  27.             ans="matches";
  28.       else  ans="doesn't match";
  29.       printf("%s\n",ans);
  30. }
  31.  
  32. int match(fn,tn) char *fn; char *tn;
  33. {
  34.       int fr,lo,hi;
  35.  
  36.       while((*tn!=0) && (*fn!=0))
  37.       {
  38.             switch(*tn)
  39.             {
  40.             case '*' :
  41.                   tn++;
  42.                   if (*tn==0)
  43.                         return(-1);
  44.                   while((*fn!=0)&&((fr=match(fn,tn))==0))
  45.                         fn++;
  46.                   if (*fn==0)
  47.                         return(0);
  48.                   else  return(fr);
  49.                   break;
  50.             case '[' :
  51.                   tn++;
  52.                   if (*tn==0)
  53.                         return(0);
  54.                   else  lo=*tn++;
  55.                   if (*tn!='-')
  56.                         return(0);
  57.                   else  tn++;
  58.                   if (*tn==0)
  59.                         return(0);
  60.                   else  hi=*tn++;
  61.                   if (*tn!=']')
  62.                         return(0);
  63.                   else  tn++;
  64.                   if ((*fn>=lo)&&(*fn<=hi))
  65.                         fn++;
  66.                   else  return(0);
  67.                   break;
  68.             case '?' :
  69.                   tn++;
  70.                   if (*fn!=0)
  71.                         fn++;
  72.                   else  return(0);
  73.                   break;
  74.             default  :
  75.                   if (*tn==*fn)
  76.                   {
  77.                         tn++;
  78.                         fn++;
  79.                   }
  80.                   else  return(0);
  81.             }
  82.       }
  83.       while(*tn=='*')
  84.             tn++;
  85.       if ((*tn==0)&&(*fn==0))
  86.             return(-1);
  87.       else  return(0);
  88. }
  89.